var uuid = require('node-uuid');

var AppInfoDao = require("../dao/AppInfoDao");
var logger = new require("../log/Logger")("AppInfoManager");
var appInfoMap = new Map();

var AppInfoManager = {
    getAppInfoList : function(){
        return AppInfoDao.getAppInfoList();
    },
    getAppInfoByAppId : function(appId){
        var appInfo = appInfoMap.get(appId);
        if(appInfo != null){
            return Promise.resolve(appInfo);
        }else{
            return new Promise(function(reslove, reject){
                AppInfoDao.getAppInfo(appId).then(function(appInfoRes){
                    appInfoMap.set(appId, appInfoRes);
                    reslove(appInfoRes);
                }, reject).catch(function(err){
                    logger.error(err);
                });
            });
        }
    },
    addNewAppInfo : function(appName, appId){
        var appInfo = {
            appName : appName,
            appId : appId,
            appKey : uuid.v1(),
            logSchemaName : "log_collection_" + appId
        }

        return AppInfoDao.insertAppInfo(appInfo);
    },
    updateAppInfo : function(oldAppName, newAppNme){
        var appInfo = {
            appName : appName,
            appId : appId,
            appKey : uuid.v1(),
            logSchemaName : "log_collection_" + appId
        }
        return AppInfoDao.updateAppInfo(appInfo);
    }
}

module.exports = AppInfoManager;
